package com.example.javademo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class FormActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_form);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        CheckBox checkbox = findViewById(R.id.checkBox);
        checkbox.setChecked(false);
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            boolean ischecked = checkbox.isChecked();
//            打印ischecked
            Log.d("ischecked", "ischecked:" + ischecked);
            if (isChecked) {
                buttonView.setText("Checked");
            } else {
                buttonView.setText("Not checked");
            }
        });

        // 获取 RadioGroup 实例
        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        radioGroup.clearCheck();
        RadioButton radioButton = findViewById(R.id.radioButton);
        radioButton.setChecked(true);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            /*
            * group: RadioGroup 实例
            * checkedId: 选中的 RadioButton 的 id
            * */
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                String text = radioButton.getText().toString();
                Log.d("radioButton", "text:" + text);
            }
        });


        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /*
            * onProgressChanged: 进度改变时调用
            * progress: 当前进度
            * fromUser: 是否是用户手动改变的
            * */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("onProgressChanged: ", String.valueOf(fromUser));
            }


            /*
            *
            * onStartTrackingTouch: 开始拖动时调用            *
            * */
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.d("onStartTrackingTouch: ", String.valueOf(true));
            }

            /*
            * onStopTrackingTouch: 停止拖动时调用
            * */
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d( "onStopTrackingTouch: ", String.valueOf(true));

            }
        });


        Button button = findViewById(R.id.buttonGo);

        /*
        * 获取上个页面传递的数据
        * */
        Intent intent = getIntent();
//        String name = intent.getStringExtra("name");
//        int age = intent.getIntExtra("age", 0);
//        Log.d("TAG", "name:" + name + "age:" + age);
//        TextView textView = findViewById(R.id.textViewForm);
//        textView.setText("姓名：" + name + "年龄：" + age);

        /*
        * getSerializableExtra
        * 获取序列化对象
        * */
        Student student = (Student) intent.getSerializableExtra("student");
        if (student != null) {
            TextView textView = findViewById(R.id.textViewForm);
            textView.setText("姓名：" + student.name + "年龄：" + student.age);
        }
        Log.d("student", "student:" + student.name + "age:" + student.age);

    }


    public void onClickBtn(View view) {
        Log.d("TAG", "clickBtn: ");
//      跳转到ButtonActivity
        /*
        * 第一个参数：当前Activity
        * 第二个参数：要跳转的Activity
        * */
//        Intent intent = new Intent(this, ButtonActivity.class);
//        startActivity(intent);
        /*
        * 传递表单数据，返回给上个页面
        * */
        /*Intent intent = new Intent();
//        putExtra 方法用于向 Intent 中添加键值对数据
        intent.putExtra("name", "张三");
        setResult(RESULT_OK, intent);
//        关闭当前Activity
        finish();*/

        /*
        * 传递失败，返回给上个页面
        * */
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }
}